/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.library.cards;


import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.utils.AnimProviderUtil;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.app.Context;

import static ohos.agp.components.Component.*;


/**
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class ProgressCard extends Card {

    /**
     * Progress View
     */
    private Component mProgressView;

    /**
     * Resource Id used which identifies the progressBar
     */
    protected int progressBarId = ResourceTable.Id_card_native_progressbar;

    /**
     * An identifier for the layout resource to inflate when the ViewStub becomes visible
     */
    protected int progressBarViewStubLayoutId = ResourceTable.Layout_native_base_progress;

    /**
     * Indicates if the progressBar feature is enabled
     */
    protected boolean useProgressBar = false;

    /**
     * Internal flag to indicate if the list is shown
     */
    private boolean mCardShown;


    public ProgressCard(Context context) {
        super(context);
    }

    public ProgressCard(Context context, int innerLayout) {
        super(context, innerLayout);
    }

    @Override
    public void setupInnerViewElements(ComponentContainer parent, Component view) {
        super.setupInnerViewElements(parent, view);
        internalSetupProgressBar(parent, view);
    }

    /**
     * Setup the Progress Bar view.
     *
     * @param parent mainContentLayout
     * @param view   innerView
     */
    @SuppressWarnings("UnusedParameters")
    private void internalSetupProgressBar(ComponentContainer parent, Component view) {
        if (useProgressBar && mProgressView == null) {
            mProgressView = ((Component) getCardView()).findComponentById(getProgressBarId());
            mCardShown = true;
            if (mProgressView != null) {
                setProgressView(mProgressView);
            }
        }
    }

    // -------------------------------------------------------------
    // Progress bar
    // -------------------------------------------------------------

    /**
     * When the current adapter is loading data, the LinearListView can display a special
     * progress Bar.
     *
     * @return The view to show if the adapter is the progress bar is enabled.
     */
    public Component getProgressView() {
        return mProgressView;
    }

    /**
     * Sets the view to show as progress bar
     * @param progressView progressView
     */
    public void setProgressView(Component progressView) {
        mProgressView = progressView;
        useProgressBar = progressView != null;
    }

    /**
     * Updates the status of the list and the progress bar.
     *
     * @param shownCard indicates if the list has to be shown
     * @param animate   indicates to use an animation between view transition
     */
    public void updateProgressBar(boolean shownCard, boolean animate) {
        if (isUseProgressBar()) {
            if (mCardShown == shownCard) {
                return;
            }
            mCardShown = shownCard;

            if (shownCard) {
                if (animate) {
                    AnimProviderUtil.getFadeOut(mProgressView.createAnimatorProperty()).start();
                    AnimProviderUtil.getFadeIn(getCardView().getInternalMainCardLayout().createAnimatorProperty()).start();
                }
                getCardView().getInternalMainCardLayout().setVisibility(VISIBLE);
                mProgressView.setVisibility(HIDE);

            } else {
                if (animate) {
                    AnimProviderUtil.getFadeIn(mProgressView.createAnimatorProperty()).start();
                    AnimProviderUtil.getFadeOut(getCardView().getInternalMainCardLayout().createAnimatorProperty()).start();
                }
                mProgressView.setVisibility(VISIBLE);
                getCardView().getInternalMainCardLayout().setVisibility(INVISIBLE);
            }
        }
    }

    // -------------------------------------------------------------
    // Getter and setter
    // -------------------------------------------------------------

    private boolean isUseProgressBar() {
        if (mProgressView != null)
            return useProgressBar;
        else
            return false;
    }

    /**
     * Sets the flag to enable and disable the progress bar
     *
     * @param useProgressBar
     */
    public void setUseProgressBar(boolean useProgressBar) {
        this.useProgressBar = useProgressBar;
    }

    /**
     * Returns the resource Id used which identifies the ProgressBar
     *
     * @return the resource Id used which identifies the ProgressBar
     */
    public int getProgressBarId() {
        return progressBarId;
    }

    /**
     * Sets the resource Id used which identifies the ProgressBar
     *
     * @param progressBarId resource Id used which identifies the ProgressBar
     */
    public void setProgressBarId(int progressBarId) {
        this.progressBarId = progressBarId;
    }


    /**
     * Returns the identifier for the layout resource to inflate when the ViewStub used by the ProgressBar becomes visible
     * It is used only if the {@see useProgressBar) is setted to true and the {@see mProgressView} is a {@link }.
     *
     * @return layoutId
     */
    public int getProgressBarViewStubLayoutId() {
        return progressBarViewStubLayoutId;
    }

    /**
     * Sets the identifier for the layout resource to inflate when the ViewStub used by the ProgressBar becomes visible
     *
     * @param progressBarViewStubLayoutId
     */
    public void setProgressBarViewStubLayoutId(int progressBarViewStubLayoutId) {
        this.progressBarViewStubLayoutId = progressBarViewStubLayoutId;
    }
}
